#include "EboRectangleRenderer.h"
#include <glad/glad.h>

EboRectangleRenderer::EboRectangleRenderer()
{
    this->vertices = {
        0.5f,  0.5f,  0.0f, // top right
        0.5f,  -0.5f, 0.0f, // bottom right
        -0.5f, -0.5f, 0.0f, // bottom left
        -0.5f, 0.5f,  0.0f  // top left
    };

    /**
    指定顶点的顺序来把4个顶点组合成2个三角形
    */
    this->indices = {
        // note that we start from 0!
        0, 1, 3, // first triangle
        1, 2, 3  // second triangle
    };
}

void EboRectangleRenderer::prepareVAO()
{

    // 1、创建EBO对象
    unsigned int EBO;
    glGenBuffers(1, &EBO);

    // 2、绑定EBO
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);

    // 3、拷贝顶点顺序索引到EBO中
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(int),
                 this->indices.data(), GL_STATIC_DRAW);

    unsigned int VBO;
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * this->vertices.size(),
                 this->vertices.data(), GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float),
                          (void*)0);
    glEnableVertexAttribArray(0);
}

void EboRectangleRenderer::render() {
    glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
}